import Axios from 'axios'
import router from '@/router/index.js'
const axios = Axios.create({
	// baseURL: process.env.NODE_ENV === 'development' ? '' : '',
})
/* resful语法 */
const map = {
	get: 'view',
	post: 'add',
	put: 'edit',
	delete: 'delete'
}
// 添加请求拦截器
axios.interceptors.request.use((config) => {
	console.log(config);
	const currentUrl = config.url
	if(currentUrl!=='/login'){
		config.headers.Auchorization = window.sessionStorage.getItem('token')
	}
	const method = config.method
	const action = map[method]
	const currentRights = router.currentRoute.meta.ah;
	if (config.url != '/login') {
		if (action && currentRights.indexOf(action) === -1) {
			alert('您没有权限此操作')
			return Promise.reject(new Error('没有权限'))
		}
	}
	return config
})


// 添加响应拦截器
axios.interceptors.response.use(function(response) {
	// 对响应数据做点什么

	return response;
}, function(error) {
	// 对响应错误做点什么
	return Promise.reject(error);
});
export default (url, method = 'get', data = {}) => {
	return axios({
		url,
		method,
		data
	})
}
